home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / MacWT 0.04 / wt / fixed.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  3.3 KB  |  120 lines  |  [TEXT/MMCC]

  1. /*
  2. **  wt -- a 3d game engine
  3. **
  4. **  Copyright (C) 1994 by Chris Laurel
  5. **  email:  claurel@mr.net
  6. **  snail mail:  Chris Laurel, 5700 W Lake St #208,  St. Louis Park, MN  55416
  7. **
  8. **  This program is free software; you can redistribute it and/or modify
  9. **  it under the terms of the GNU General Public License as published by
  10. **  the Free Software Foundation; either version 2 of the License, or
  11. **  (at your option) any later version.
  12. **
  13. **  This program is distributed in the hope that it will be useful,
  14. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. **  GNU General Public License for more details.
  17. **
  18. **  You should have received a copy of the GNU General Public License
  19. **  along with this program; if not, write to the Free Software
  20. **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22.  
  23. #ifndef FIXED_H_
  24. #define FIXED_H_
  25.  
  26. #include "limits.h"
  27.  
  28. /* fixed point conversions */
  29. #define INT_TO_FIXED(i) ((i) << 16)
  30. #define FIXED_TO_INT(f) ((f) >> 16)
  31. #define FIXED_TO_FLOAT(f) (((double) (f)) / 65536.0)
  32. #define FLOAT_TO_FIXED(f) ((fixed) (f * 65536.0))
  33.  
  34. /* functions */
  35. #define FIXED_ABS(f) ((f) < 0 ? -(f) : (f))
  36. #define FIXED_TRUNC(f) ((f) & 0xffff0000)
  37. #define FIXED_SIGN(f) ((unsigned int) (f) >> 31)
  38. #define FIXED_PRODUCT_SIGN(f, g) ((unsigned int) ((f) ^ (g)) >> 31)
  39. #define FIXED_HALF(f) ((f) / 2)
  40. #define FIXED_DOUBLE(f) ((f) << 1)
  41.  
  42. /* perform integer scaling of a fixed point number */
  43. #define FIXED_SCALE(f, i) ((f) * (i))
  44.  
  45.  
  46. /* fixed point constants */
  47. #define FIXED_ZERO     (INT_TO_FIXED(0))
  48. #define FIXED_ONE      (INT_TO_FIXED(1))
  49. #define FIXED_ONE_HALF (FIXED_HALF(FIXED_ONE))
  50. #define FIXED_PI       (FLOAT_TO_FIXED(3.14159265))
  51. #define FIXED_2PI      (FLOAT_TO_FIXED(6.28318531))
  52. #define FIXED_HALF_PI  (FLOAT_TO_FIXED(1.57079633))
  53. #define FIXED_MIN      LONG_MIN
  54. #define FIXED_MAX      LONG_MAX
  55.  
  56. /* we need this for kludges to avoid fixed point division overflow */
  57. #define FIXED_EPSILON  ((fixed) 0x100)
  58.  
  59. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  60. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  61.  
  62. typedef long fixed;
  63.  
  64. extern fixed fix_sqrt(fixed);
  65.  
  66. #ifndef fixmul
  67. #if defined(__GNUC__) && defined(ARCH_i86)
  68. static inline fixed fixmul(fixed r1, fixed r2)
  69. {
  70.      fixed result;
  71.  
  72.      __asm__ ("imull %2\n\t"
  73.           "shrd  $16, %%edx, %%eax\n\t"
  74.           :"=a" (result):"a" (r1), "d" (r2):"eax", "edx");
  75.  
  76.      return result;
  77. }
  78.  
  79.  
  80. static inline fixed fixmul2_30(fixed r1, fixed r2)
  81. {
  82.      fixed result;
  83.  
  84.      __asm__ ("imull %2\n\t"
  85.           "shrd  $30, %%edx, %%eax\n\t"
  86.           :"=a" (result):"a" (r1), "d" (r2):"eax", "edx");
  87.  
  88.      return result;
  89. }
  90.  
  91.  
  92. static inline fixed fixdiv(fixed dividend, fixed divisor)
  93. {
  94.      fixed result;
  95.  
  96.      if (divisor == FIXED_ZERO)
  97.       fatal_error("divide by zero");
  98.  
  99.      /* no checking for overflow is done yet */
  100.      __asm__("movl %%edx, %%eax\n\t"
  101.          "sar  $16, %%edx\n\t"
  102.          "shl  $16, %%eax\n\t"
  103.          "idivl %%ecx, %%eax\n\t"
  104.          :"=a" (result):"d" (dividend), "c" (divisor):"eax", "ecx", "edx");
  105.  
  106.      return result;
  107. }
  108.  
  109. #else
  110.  
  111. #define fixmul(a, b) ((fixed) (((double) (a) * (double) (b)) / 65536.0))
  112. #define fixmul2_30(a, b) ((fixed) (((double) (a) * (double) (b)) / \
  113.                    1073741824.0))
  114. #define fixdiv(a, b) ((fixed) (((double) (a) / (double) (b)) * 65536.0))
  115.  
  116. #endif    /* inline asm */
  117.  
  118. #endif    /* fixmul */
  119. #endif    /* _FIXED_H_ */
  120.